package main

import (
	"context"
	"fmt"
	ezToken "gitee.com/jinmingzhi/eth-learn/5day/build"
	"gitee.com/jinmingzhi/eth-learn/pkg/ethtool"
	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"io/ioutil"
	"math/big"
)

var (
	url    = "http://localhost:8545"
	priKey = "0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d"
)

func main() {
	client, err := ethtool.Dial(url)
	assert(err)

	tokenSupply := big.NewInt(1000000) //代币发行量
	tokenName := "HAPPY TOKEN"         //代币名称
	tokenDecimals := uint8(0)          //代币小数位数
	tokenSymbol := "HAPY"              //代币符号
	ctx := context.Background()

	credential, _ := ethtool.HexToCredential(priKey)
	chainId, err := client.NetVersion(ctx)
	assert(err)

	txOpts, err := bind.NewKeyedTransactorWithChainID(credential.PrivateKey, chainId)
	assert(err)

	address, tx, inst, err := ezToken.DeployEzToken(txOpts, client, tokenSupply, tokenName, tokenDecimals, tokenSymbol)
	assert(err)
	fmt.Println("deployed address: ", address.Hex()) //显示合约部署的地址
	fmt.Println("deploy txid: ", tx.Hash().Hex())    //显示部署交易的哈希
	ioutil.WriteFile("./build/EzToken.address", address.Bytes(), 0644)


	balance, err := inst.BalanceOf(&bind.CallOpts{
		From: credential.Address,
	}, credential.Address)

	assert(err)
	fmt.Println("balance:", balance)
}

func assert(err error) {
	if err != nil {
		panic(err)
	}
}
